package com.sunday.pinxinghui.model.index;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.banner.ConvenientBanner;
import com.sunday.common.widgets.banner.holder.ViewHolderCreator;
import com.sunday.common.widgets.ptr.PtrClassicFrameLayout;
import com.sunday.common.widgets.ptr.PtrDefaultHandler;
import com.sunday.common.widgets.ptr.PtrFrameLayout;
import com.sunday.common.widgets.ptr.PtrHandler;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.adapter.IndexAdHolder;
import com.sunday.pinxinghui.adapter.IndexMiaoShaAdapter;
import com.sunday.pinxinghui.adapter.IndexTodaysAdapter;
import com.sunday.pinxinghui.adapter.LimitTimePageAdapter;
import com.sunday.pinxinghui.adapter.LimitTimeTabAdapter;
import com.sunday.pinxinghui.adapter.TodayAdapter;
import com.sunday.pinxinghui.base.BaseLazyFragment;
import com.sunday.pinxinghui.entity.BannerImg;
import com.sunday.pinxinghui.entity.CatProduct;
import com.sunday.pinxinghui.entity.HeadData;
import com.sunday.pinxinghui.entity.IndexProduct;
import com.sunday.pinxinghui.entity.LimitTime;
import com.sunday.pinxinghui.model.main.IndexFragment;
import com.sunday.pinxinghui.model.products.MiaoShaProductListActivity;
import com.sunday.pinxinghui.model.products.ProductDetailActivity;
import com.sunday.pinxinghui.model.products.ProductsListActivity;
import com.sunday.pinxinghui.network.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Author by Damon,  on 2018/4/9.
 */

public class TodayNewFragment extends BaseLazyFragment {


    @Bind(R.id.ptr_frame)
    PtrClassicFrameLayout ptrFrame;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;


    private List<IndexProduct> productList = new ArrayList<>();
    private List<CatProduct> catProducts = new ArrayList<>();
    private List<BannerImg> bannerImgs = new ArrayList<>();
    private String qgImg="";
    private LinearLayoutManager layoutManager;
    private TodayAdapter adapter;


    public static TodayNewFragment newInstance() {
        TodayNewFragment fragment = new TodayNewFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.today_new_fragment, container, false);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        getHeadData();
    }


    private void initView() {
        adapter = new TodayAdapter(mContext, productList, catProducts, bannerImgs,qgImg);
        layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(mContext).drawable(R.drawable.shape_divider_width).build());
        recyclerView.setAdapter(adapter);
        recyclerView.setNestedScrollingEnabled(false);
        adapter.notifyDataSetChanged();
        ptrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getHeadData();
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, recyclerView, header);
            }
        });
        ptrFrame.disableWhenHorizontalMove(true);

        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.ll_title:
                        int p= (int) v.getTag(R.id.ll_title_id);
                        if (!productList.isEmpty()) {
                            if (p == 1) {
                                intent = new Intent(mContext, MiaoShaProductListActivity.class);
                                startActivity(intent);
                            } else {
                                ((IndexFragment) getParentFragment()).setPage(catProducts.get(p - 2).getCatId());
                            }
                        } else {
                            ((IndexFragment) getParentFragment()).setPage(catProducts.get(p - 1).getCatId());
                        }
                        break;
                    case R.id.iv_cart:
                        int p1= (int) v.getTag(R.id.iv_cart_id);

                        break;
                }

            }
        });

    }


    //轮播图和秒杀产品
    private void getHeadData() {
        Call<ResultDO<HeadData>> call = ApiClient.getApiAdapter().getIndexHeadData();
        call.enqueue(new Callback<ResultDO<HeadData>>() {
            @Override
            public void onResponse(Call<ResultDO<HeadData>> call, Response<ResultDO<HeadData>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ptrFrame.refreshComplete();
                if (response.body().getCode() == 0) {
                    productList.clear();
                    catProducts.clear();
                    bannerImgs.clear();
                    productList.addAll(response.body().getResult().getLimitCountProducts());
                    catProducts.addAll(response.body().getResult().getPc());
                    bannerImgs.addAll(response.body().getResult().getBanners());
                    qgImg=response.body().getResult().getQgtu();
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<HeadData>> call, Throwable t) {
                ptrFrame.refreshComplete();
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }


    @Override
    public void onResume() {
        super.onResume();
        getHeadData();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
